/**
 * 
 */
package com.xcn.siteinfo;

import java.net.*;

import net.sf.json.*;

import org.jsoup.*;
import org.jsoup.helper.*;
import org.jsoup.nodes.*;
import org.jsoup.select.*;

import com.xcn.exception.*;

/**
 * @author changmin
 *
 */
public class Url_Lookup
{
	
	/**
	 * 
	 */
	public Url_Lookup( )
	{
	}
	
	
	public String lottoLookup ( String findSite )
	{
		String siteInfo = "NOT_FOUND";
		try
		{
			Document doc = Jsoup.connect ( "http://lotto.kaisyu.com/api?method=get&gno=1" ).get ( );
			Element el = doc.body ( );
			siteInfo = el.text ( );
			JSONObject obj = ( JSONObject ) JSONSerializer.toJSON ( siteInfo );
			JSONArray nums = ( JSONArray ) obj.get ( "nums" );
			for ( int i = 0 ; i < nums.size ( ) ; i++ )
			{
				System.out.println ( nums.get ( i ) );
			}
			
		}
		catch ( Exception e )
		{
			//
		}
		return siteInfo;
	}
	
	public String googleLookup ( String findSite )
	{
		String siteInfo = "NOT_FOUND";
		try
		{
			Document doc = Jsoup.connect ( "https://www.facebook.com/#!/messages/" ).get ( );
			siteInfo = doc.toString ( );
		}
		catch ( Exception e )
		{
			//
		}
		return siteInfo;
	}

	public String naverLookup ( String findSite )
	{
		String siteInfo = "NOT_FOUND";
		try
		{
			Document doc = Jsoup.connect ( "http://search.naver.com/search.naver?sm=tab_hty.top&where=nexearch&ie=utf8&query=" + findSite + "&x=0&y=0" ).get ( );
			Elements elements = (( org.jsoup.nodes.Element ) doc).getElementsByAttributeValue ( "class", "cate" );
			for ( int i=0 ; i < elements.size ( ); i++ )
			{
				Element e = elements.get ( i );
				siteInfo = e.text ( );
			}
		}
		catch ( Exception e )
		{
			//
		}
		return siteInfo;
	}

	public String daumLookup ( String findSite )
	{
		StringBuffer siteInfo = new StringBuffer ( );
		try
		{
			Connection con = HttpConnection.connect ( new URL ( "http://search.daum.net/search?nil_suggest=btn&nil_ch=&rtupcoll=&w=site&m=&f=&lpp=&DA=SBCO&sug=&q=" + findSite ) );
			con.header ( "Cookie", "TIARA=rWOhSaNuHmHlw29erDUkHnrq5iBb_ihj7eb3rCCMCrpmoY1NSjc3C9x7uy__JCHnj_C48XGRBnc0; uvkey=UUhugq97@w4AACTe0DsAAABM; abk=y; ODT=1DVZ_BR1Z_FGKZ_NKSZ_BRDZ_WSAZ_CCBZ_; DDT=NNSZ_IIMZ_LB2Z_RNKZ_VO2Z_IVRZ_CJEZ_DICZ_GG1Z_SINZ_MS2Z_; DTQUERY=%EB%8B%A4%EC%9D%8C+%EB%94%94%EB%A0%89%ED%86%A0%EB%A6%AC" );
			Document daumDoc = con.get ( );
			
			Elements elements = daumDoc.getElementsByAttributeValue ( "class", "cont_inner" );
			if ( elements.size ( ) > 0 )
			{
				elements = elements.get ( 0 ).getElementsByAttributeValue ( "class", "f_nb" );
				for ( int i=0 ; i < elements.size ( ); i++ )
				{
					Element e = elements.get ( i );
					if ( i >= elements.size ( )-1 ) siteInfo.append ( e.text ( ) );
					else siteInfo.append ( e.text ( ) ).append ( " > " );
				}
			}
		}
		catch ( Exception e )
		{
			e.printStackTrace ( );
		}
		if ( siteInfo.length ( ) == 0 ) siteInfo.append ( "NOT_FOUND" );
		return siteInfo.toString ( );
	}
	
	/**
	 * @param args
	 * @throws XCNException 
	 */
	public static void main ( String[] args ) throws XCNException
	{
		Url_Lookup _lookup = new Url_Lookup ( );
		if ( args.length == 2 )
		{
			if ( args[0].equals ( "daum" ))
				System.out.println ( _lookup.daumLookup ( args[1] ) );
			else if ( args[0].equals ( "naver" ))
				System.out.println ( _lookup.naverLookup ( args[1] ) );
			else if ( args[0].equals ( "google" ) )
				System.out.println ( _lookup.googleLookup( args[1] ) );
			else if ( args[0].equals ( "lotto" ) )
				System.out.println ( _lookup.lottoLookup( args[1] ) );
			else
				throw new XCNException ( "This site does not support." );
		}
		else throw new XCNException ( "<Usage:URL Category Lookup> ex) naver www.xcurenet.com" );
	}
	
}


































